#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Create on May 2011

@author Wang Qiang
"""


import sys
import os
path = os.path.join(os.path.dirname(__file__), '..')
sys.path.append(path)

from QQWeiboAPI.qqAPI import QQAPI
from QQWeiboAPI import oauth
import unittest
import simplejson as json


class FriendsTest(unittest.TestCase):
    """
    Test for the friends operation
    
    friends/fanslist
    friends/idollist
    friends/blacklist
    friends/speciallist
    friends/add
    friends/del
    friends/addspecial
    friends/delspecial
    friends/addblacklist
    friends/delblacklist
    friends/check
    friends/user_fanslist
    friends/user_idollist
    friends/user_speciallist
    """

    consumer_key = ''
    consumer_secret = ''
    access_key = ''
    access_secret = ''
            
    def testConsumer(self):
        """test for loading consumer"""
        path = os.path.join(os.path.dirname(__file__), 'consumer.txt')
        f = open(path, 'r')
        consumerStr = f.read()
        f.close()
        consumer = json.loads(consumerStr)
        self.consumer_key = consumer['key']
        self.consumer_secret = consumer['secret']
        self.assertEqual(len(self.consumer_key), 32)
        self.assertEqual(len(self.consumer_secret), 32)
   
    def testAccessToken(self):
        """test for loading access_token"""
        path = os.path.join(os.path.dirname(__file__), 'access_token.txt')
        f = open(path, 'r')
        access_tokenStr = f.read()
        f.close()
        access_token = json.loads(access_tokenStr)
        self.access_key = access_token['key']
        self.access_secret = access_token['secret']
        self.assertEqual(len(self.access_key), 32)
        self.assertEqual(len(self.access_secret), 32)
    
    def testFriendsFanslist(self):
        """test for friends/fanslist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.fanslist_friends(format='json', reqnum=30, \
                                      startindex=0)
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick
    
    def testFriendsIdollist(self):
        """test for friends/idollist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.idollist_friends(format='json', reqnum=30, \
                                      startindex=0)
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick

    def testFriendsBlacklist(self):
        """test for friends/blacklist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.blacklist_friends(format='json', reqnum=30, \
                                      startindex=0)
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick


    def testFriendsSpeciallist(self):
        """test for friends/speciallist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.speciallist_friends(format='json', reqnum=30, \
                                      startindex=0)
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick

    def testFriendsAdd(self):
        """test for friends/add"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.add_friends(format='json', name='jeff')
        self.assertEqual(res.ret, 0)

    def testFriendsDel(self):
        """test for friends/del"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.del_friends(format='json', name='jeff')
        self.assertEqual(res.ret, 0)
    
    def testFriendsAddspecial(self):
        """test for friends/addspecial"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.addspecial_friends(format='json', name='pony')
        print res.msg
        self.assertEqual(res.ret, 0)

    def testFriendsDelspecial(self):
        """test for friends/delspecial"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.delspecial_friends(format='json', name='pony')
        print res.msg
        self.assertEqual(res.ret, 0)

    def testFriendsAddblacklist(self):
        """test for friends/addblacklist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.addblacklist_friends(format='json', name='jeff')
        print res.msg
        self.assertEqual(res.ret, 0)

    def testFriendsDelblacklist(self):
        """test for friends/delblacklist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.delblacklist_friends(format='json', name='jeff')
        print res.msg
        self.assertEqual(res.ret, 0)

    def testFriendsCheck(self):
        """test for friends/check"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        myNames='jeff,pony'
        res = testAPI.check_friends(format='json', names=myNames, flag=0)
        self.assertEqual(res.ret, 0)
        res = testAPI.check_friends(format='json', names=myNames, flag=1)
        self.assertEqual(res.ret, 0)
        res = testAPI.check_friends(format='json', names=myNames, flag=2)
        self.assertEqual(res.ret, 0)

    def testFriendsUserFanslist(self):
        """test for friends/user_fanslist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.user_fanslist_friends(format='json', reqnum=30, \
                                      startindex=0, name='pony')
        print res.msg
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick
    
    def testFriendsUserIdollist(self):
        """test for friends/user_idollist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.user_idollist_friends(format='json', reqnum=30, \
                                      startindex=0, name='pony')
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick

    def testFriendsUserSpeciallist(self):
        """test for friends/user_speciallist"""
        self.testConsumer()
        self.testAccessToken()
        consumer = oauth.OAuthToken(self.consumer_key, self.consumer_secret)
        access_token = oauth.OAuthToken(self.access_key, self.access_secret)
        testAPI = QQAPI(consumer, access_token)
        res = testAPI.user_speciallist_friends(format='json', reqnum=30, \
                                      startindex=0, name='pony')
        self.assertEqual(res.ret, 0)
        print res.data.info[0].nick


if __name__ == '__main__':
    #unittest.main()
    fast = unittest.TestSuite()
    fast.addTest(FriendsTest('testFriendsUserSpeciallist'))
    unittest.TextTestRunner().run(fast)
